package com.med.meddataintegrator.doctor.service.Impl;

import com.med.meddataintegrator.common.enums.UserRole;
import com.med.meddataintegrator.common.mapper.UserDoMapper;
import com.med.meddataintegrator.common.mapper.UserRoleDoMapper;
import com.med.meddataintegrator.common.pojo.dos.DoctorDo;
import com.med.meddataintegrator.common.pojo.dos.UserDo;
import com.med.meddataintegrator.common.pojo.dos.UserRoleDo;
import com.med.meddataintegrator.common.pojo.dto.doctor.DoctorParam;
import com.med.meddataintegrator.common.pojo.dto.doctor.DoctorQueryParams;
import com.med.meddataintegrator.common.mapper.DoctorMapper;
import com.med.meddataintegrator.common.pojo.vo.doctor.DoctorVo;

import com.med.meddataintegrator.doctor.service.doctorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.Transient;
import java.util.List;

@Slf4j
@Service
public class doctorServiceImpl implements doctorService {


    @Autowired
    private DoctorMapper doctorMapper;


    @Autowired
    private UserDoMapper userDoMapper;


    @Autowired
    private UserRoleDoMapper userRoleDoMapper;



    @Override
    public List<DoctorVo> selectDoctor(DoctorQueryParams doctorQueryParams) {
        log.debug("查询医生业务：{}",doctorQueryParams);
        List<DoctorVo> doctorVoList = doctorMapper.select(doctorQueryParams);
        return doctorVoList;
    }

    @Override
    public DoctorVo selectDoctorById(Long id) {
        log.debug("查询医生业务：{}",id);
        DoctorVo doctorVo = doctorMapper.selectByIdDoctor(id);
        return doctorVo;
    }

    @Override
    public void updateDoctor(DoctorParam doctorParam) {
        log.debug("更新医生业务：{}",doctorParam);
        DoctorDo doctorDo = new DoctorDo();
        BeanUtils.copyProperties(doctorParam,doctorDo);
        doctorDo.setUserId(doctorParam.getId());
        doctorMapper.updateDoctor(doctorDo);
    }

    @Override
    public void delDoctor(Long id) {
        log.debug("删除医生业务：{}",id);
        doctorMapper.delDoctor(id);
    }

    @Override
    @Transient
    public void addDoctor(DoctorParam doctorParam) {
        log.debug("添加医生业务：{}",doctorParam);
        // 清洗数据
        DoctorDo doctorDo = new DoctorDo();
        UserDo userDo = new UserDo();
        UserRoleDo userRoleDo = new UserRoleDo();


        // 医生表
        doctorDo.setAddress(doctorParam.getAddress());
        doctorDo.setCertId(doctorParam.getCertId());
        doctorDo.setName(doctorParam.getDockerName());
        doctorDo.setSex(doctorParam.getSex());
        doctorDo.setAge(doctorParam.getAge());
        int dockerId =doctorMapper.insert(doctorDo);


        // 角色表
        userDo.setPassword(doctorParam.getPassword());
        userDo.setUsername(doctorParam.getName());
        userDo.setRole(UserRole.DOCTOR.getRole());
        int userId = userDoMapper.insert(userDo);


        // 角色权限关联标
        userRoleDo.setRoleId(dockerId);
        userRoleDo.setUserId(userId);
        userRoleDoMapper.insert(userRoleDo);

    }
}
